﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class LocationSetController : Controller
    {
        //
        // GET: /LocationSet/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetRepo _LocationSetRepoRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepoRepo.GetLocationSet();
            return View(_LocationSets);
        }

        //
        // GET: /LocationSet/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetRepo _LocationSetRepoRepo = new LocationSetRepo();
            LocationSetModels _LocationSet = new LocationSetModels();
            _LocationSet = _LocationSetRepoRepo.GetLocationSetById(id);
            return View(_LocationSet);
        }

        //
        // GET: /LocationSet/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetModels _LocationSet = new LocationSetModels();
           
            MerchantRepo _MerchantRepo = new MerchantRepo();
            IEnumerable<MerchantModels> _Merchants = _MerchantRepo.GetMerchant();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _Merchants
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.MerchantId == selectedId),
                                                         Text = s.NameEng,
                                                         Value = s.MerchantId.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _LocationSet.MerchantSelect = selectlist;
            return View(_LocationSet);
        } 

        //
        // POST: /LocationSet/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                LocationSetRepo _LocationSetRepoRepo = new LocationSetRepo();
                LocationSetModels _LocationSet = new LocationSetModels();

                _LocationSet.LocationSetCode = collection["LocationSetCode"];
                _LocationSet.MerchantId = SF.GetInt32(collection["MerchantId"]);
                _LocationSet.LocSetNameTh = collection["LocSetNameTh"];
                _LocationSet.LocSetNameEng = collection["LocSetNameEng"];
                _LocationSet.CreateBy = (string)Session["User"];
                bool chekdup = _LocationSetRepoRepo.CheckDub(_LocationSet.LocationSetCode);

                if (_LocationSet.LocationSetCode == "")
                {
                    TempData["errorname0"] = "ยังไม่ป้อนข้อมูลชื่อ Location code";
                    return RedirectToAction("Create");
                }
                else if (chekdup)
                {
                    TempData["errorname0"] = "Location code มีการใช้งานแล้ว";
                    return RedirectToAction("Create");
                }
                else if (_LocationSet.LocSetNameTh == "")
                {
                    TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อ Location ภาษาไทย";
                    return RedirectToAction("Create");
                }
                else if (_LocationSet.LocSetNameEng == "")
                  {
                      TempData["errorname2"] = "ยังไม่ป้อนข้อมูลชื่อ Location ภาษาอังกฤษ";
                      return RedirectToAction("Create");
                  }

                  else
                  {

                      bool result = _LocationSetRepoRepo.Create(_LocationSet);

                      return RedirectToAction("Index");
                  }
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /LocationSet/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetRepo _LocationSetRepoRepo = new LocationSetRepo();
            LocationSetModels _LocationSet = new LocationSetModels();
            _LocationSet = _LocationSetRepoRepo.GetLocationSetById(id);
            MerchantRepo _MerchantRepo = new MerchantRepo();
            IEnumerable<MerchantModels> _Merchants = _MerchantRepo.GetMerchant();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _Merchants
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.MerchantId == selectedId),
                                                         Text = s.NameEng,
                                                         Value = s.MerchantId.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _LocationSet.MerchantSelect = selectlist;
            return View(_LocationSet);
        }

        //
        // POST: /LocationSet/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                LocationSetRepo _LocationSetRepoRepo = new LocationSetRepo();
                LocationSetModels _LocationSet = new LocationSetModels();
                _LocationSet.LocSetId = id;
                _LocationSet.LocationSetCode = collection["LocationSetCode"];
                _LocationSet.MerchantId =SF.GetInt32( collection["MerchantId"]);
                _LocationSet.LocSetNameTh = collection["LocSetNameTh"];
                _LocationSet.LocSetNameEng = collection["LocSetNameEng"];
                _LocationSet.UpdateBy = (string)Session["User"];
                 if (_LocationSet.LocSetNameTh == "")
                {
                    TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อ Location ภาษาไทย";
                    return RedirectToAction("Edit");
                }
                 else if (_LocationSet.LocSetNameEng == "")
                 {
                     TempData["errorname2"] = "ยังไม่ป้อนข้อมูลชื่อ Location ภาษาอังกฤษ";
                     return RedirectToAction("Edit");
                 }

                 else
                 {
                     bool result = _LocationSetRepoRepo.Edit(_LocationSet);

                     return RedirectToAction("Index");
                 }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /LocationSet/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetRepo _LocationSetRepoRepo = new LocationSetRepo();
            LocationSetModels _LocationSet = new LocationSetModels();
            _LocationSet = _LocationSetRepoRepo.GetLocationSetById(id);
            return View(_LocationSet);
        }

        //
        // POST: /LocationSet/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                LocationSetRepo _LocationSetRepoRepo = new LocationSetRepo();
                LocationSetModels _LocationSet = new LocationSetModels();
                _LocationSet.LocSetId = id;
                _LocationSet.UpdateBy = (string)Session["User"];
                bool result = _LocationSetRepoRepo.Delete(_LocationSet);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
